package com.citywithincity.db.validate.oracle;

import java.sql.ResultSetMetaData;

import com.citywithincity.db.DbValidateException;
import com.citywithincity.db.interfaces.IColumnDataValidate;

/**
 * 一个是默认值得判断，一个是长度判断
 * @author Randy
 *
 */
public class NVarchar2Validate implements IColumnDataValidate {
	private int len;
	private boolean isNullable;
	
	public NVarchar2Validate(int len,int isNullable) {
		this.len = len;
		if(isNullable==ResultSetMetaData.columnNullable){
			this.isNullable = true;
		}else{
			this.isNullable = false;
		}
		
	}
	
	@Override
	public Object validateData(Object data) throws DbValidateException {
		if(data==null && !isNullable){
			throw new DbValidateException("不能为空");
		}
		String value = String.valueOf(data);
		if(value.length() > len){
			throw new DbValidateException(String.format("长度应该小于%d个",len));
		}
		return value;
	}

}
